package org.fuys.etp.core.checker.impl;

import org.fuys.etp.core.checker.EtpUpdateChecker;
import org.fuys.etp.core.model.res.TpConfigCheckResult;
import org.fuys.etp.core.model.res.TpStatusResult;
import org.fuys.etp.core.model.type.EtpTaskTrendTypeEnum;
import org.fuys.etp.core.model.vo.TpBaseConfigVO;
import org.fuys.etp.core.model.vo.TpRegulationStrategyVO;
import org.fuys.etp.external.resource.ISystemInfo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @projectName: fuys-etp-final
 * @package: org.fuys.etp.core.checker.impl
 * @className: SystemResourceChecker
 * @author: WangJing
 * @description: 系统资源检查器
 * @date: 2025/2/18 22:19
 * @version: 1.0
 */
@Component
public class SystemResourceChecker implements EtpUpdateChecker {

    @Resource
    private ISystemInfo systemInfo;

    @Override
    public void checkHandle(TpRegulationStrategyVO strategy, TpStatusResult result, TpConfigCheckResult check) {
        final TpBaseConfigVO baseConfig = result.getConfigVO().getBaseConfig();
        if(systemInfo.getCpuUsage() > baseConfig.getCpuPointLimit() ||
                systemInfo.getMemoryUsage() > baseConfig.getMemoryPointLimit()){
            //如果已经超出了限制 则不会进行扩容 这里要判断线程池趋势
            final String poolType = result.getPoolType();
            if(EtpTaskTrendTypeEnum.INCREASE.getType().equals(result.getTpStatusVO().getTaskTrend())){
                check.setNeedChange(false);
                check.setExtInfo("达到CPU或内存限制 无法更改配置");
            }
        }
        //否则不做任何限制 doNothing
    }
}
